<?php

namespace common\models\db;

use Yii;
use common\components\wtools\tools\ArrayHelper;
use yii\behaviors\TimestampBehavior;

/**
 * This is the model class for table "{{%skus}}".
 *
 * @author
 *
 * @property Spus $spu
 * @property SpuHasRemoteFreights[] $remoteFreights
 * @property SkuHasAttrvs $hasAttrvs
 * @property Files $coverFile
 */
class Skus extends \common\models\db\tables\Skus
{
    const SCENARIO_TEST = 'test';


    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors = ArrayHelper::merge($behaviors, [
            'timestamp' => [
                'class' => TimestampBehavior::class,
            ],
        ]);
        return $behaviors;
    }

    public function attributeLabels()
    {
        $attributeLabels = parent::attributeLabels();
        $attributeLabels = ArrayHelper::merge($attributeLabels, []);
        return $attributeLabels;
    }

    public function rules()
    {
        $rules = parent::rules();
        foreach ($rules as $k => $v) {
            if ($v[1] == 'required'){
                $rules[$k][0] = array_diff($rules[$k][0], ['created_at', 'updated_at', 'created_by', 'updated_by']);
            }
        }
        $rules = ArrayHelper::merge($rules, [
//            [[], 'required', 'on' => self::SCENARIO_TEST],
        ]);
        return $rules;
    }

    public function scenarios()
    {
        $scenarios = parent::scenarios();
        $scenarios = ArrayHelper::merge($scenarios, [
            self::SCENARIO_TEST => [],
        ]);
        return $scenarios;
    }


    /**
     * @return false|mixed|Skus[]
     */
    public static function getAllWithCache()
    {
        return Yii::$app->cache->getOrSet("db-Skus-getAllWithCache", function () {
            return static::find()->all();
        }, null);
    }

    /**
     * @param $id
     * @return false|Skus|mixed
     */
    public static function getByIdWithCache($id)
    {
        return Yii::$app->cache->getOrSet("db-Skus-getByIdWithCache/{$id}", function () use ($id) {
            return static::findOne($id);
        }, null);
    }

    public function deleteCaches()
    {
        Yii::$app->cache->delete("db-Skus-getAllWithCache");
        Yii::$app->cache->delete("db-Skus-getByIdWithCache/{$this->id}");
    }

    public function afterSave($insert, $changedAttributes)
    {
        parent::afterSave($insert, $changedAttributes);
        $this->deleteCaches();
    }

    public function afterRefresh()
    {
        parent::afterRefresh();
        $this->deleteCaches();
    }

    public function afterDelete()
    {
        parent::afterDelete();
        $this->deleteCaches();
    }


    # relations

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getSpu()
    {
        return $this->hasOne(Spus::class, ['id' => 'spu_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getRemoteFreights()
    {
        return $this->hasMany(SpuHasRemoteFreights::class, ['spu_id' => 'spu_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getHasAttrvs()
    {
        return $this->hasMany(SkuHasAttrvs::class, ['sku_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getCoverFile()
    {
        return $this->hasOne(Files::class, ['id' => 'cover_file_id']);
    }

    # static funs

    public function test()
    {
        $test = self::instance();
        $test->setScenario(self::SCENARIO_TEST);
        $test->save();
        var_dump($test->toArray());
    }

    # funs
}
